﻿using System.Web.Mvc;
using System.Web.Routing;
using Higgs.Web.AtomPub.Demo.DependencyResolution;
using Higgs.Web.AtomPub.Demo.Models;
using StructureMap;
using Higgs.Web.AtomPub.Models;

namespace Higgs.Web.AtomPub.Demo
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        public static void RegisterType()
        {
            var container = IoC.Initialize();
            DependencyResolver.SetResolver(new SmDependencyResolver(container));

            ObjectFactory.Configure(x =>
            {
                x.For<IEntryRepository>().Use<BaseEntryRepository<BlogEntities, Entry, Category, MediaItem>>();
                x.For<IMediaRepository>().Use<BaseMediaRepository<BlogEntities, Entry, Category, MediaItem>>();
                x.For<IEntry>().Use<Entry>();
                x.For<IMediaItem>().Use<MediaItem>();
                x.For<ICategory>().Use<Category>();
            });
        }

        protected void Application_Start()
        {
            RegisterType();
            AtomPublishingApp.Start();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            AtomPublishingApp.EntryRepository = new BaseEntryRepository<BlogEntities, Entry, Category, MediaItem>();
        }
    }
}